﻿// ******************************************************************************************************
// <copyright file="Interval.cs" company="Caroso.inc">
//     Copyright (c) Carlos Osorio All rights reserved.
// </copyright>
// ******************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http:// www.mozilla.org/MPL/ 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either express or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
// 
// The Original Code is this 
// 
// The Initial Developer of this version of the Original Code is Carlos Osorio carosocali@gmail.com
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
// Change Log: 
// Date           Changed By      Notes
// 6 jun 2011    Carlos Osorio   Inital upload t
// ******************************************************************************************************

namespace GeostatisticalTool.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// This class evaluates ranges
    /// </summary>
    public class Interval
    {
        /// <summary>
        /// Low limit of Interval
        /// </summary>
        public readonly double Low;
        
        /// <summary>
        /// High limit of Interval
        /// </summary>
        public readonly double High;

        /// <summary>
        /// Initializes a new instance of the Interval class
        /// </summary>
        /// <param name="low">Low value of the interval</param>
        /// <param name="high">High value of the interval</param>
        public Interval(
            double low, double high)
        {
            if (this.Less(high, low))
            {
                throw new ArithmeticException("Illegal argument");
            }

            this.Low = low;
            this.High = high;
        }

        /// <summary>
        /// Evaluate if x is contained in the interval
        /// </summary>
        /// <param name="x">Value to be evaluated</param>
        /// <returns>True if the value is contained</returns>
        public bool Contains(
            double x)
        {
            return !this.Less(x, this.Low) && !this.Less(this.High, x);
        }

        /// <summary>
        /// This evaluates if That(interval) is intersected
        /// </summary>
        /// <param name="that">Interval to be evaluated</param>
        /// <returns>True if the interval is intersected</returns>
        public bool Intersects(
            Interval that)
        {
            if (this.Less(this.High, that.Low))
            {
                return false;
            }

            if (this.Less(that.High, this.Low))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Evaluate if two intervals are equal
        /// </summary>
        /// <param name="that">Interval to be evaluated</param>
        /// <returns>True if the interval are equal</returns>
        public bool Equals(
            Interval that)
        {
            return this.Low.Equals(that.Low) && this.High.Equals(that.High);
        }

        /// <summary>
        /// Compare if x is less of y
        /// </summary>
        /// <param name="x">value of x</param>
        /// <param name="y">value of y</param>
        /// <returns>True if x is less of y</returns>
        public bool Less(
            double x, double y)
        {
            return x.CompareTo(y) < 0;
        }

        /// <summary>
        /// Override method ToString
        /// </summary>
        /// <returns>String with the Interval</returns>
        public override string ToString()
        {
            return "[" + this.Low.ToString() + ", " + this.High.ToString() + "]";
        }
    }
}
